package com.yangcangclub.message.domain;

import com.yangcangclub.message.bean.MesgEnum;
import com.yangcangclub.message.bean.PullMsg;
import com.google.common.collect.Maps;
import com.yangcangclub.message.base.MesgBaseDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 用户个人消息
 */
@Component
public class PullMessage extends MesgBaseDomain {
    private static Logger logger = LoggerFactory.getLogger(PullMessage.class);

    /**
     *获取指定类型的消息数目
     */
    public  int  getMessgNum(int userId,MesgEnum type){

        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);

        if(type==MesgEnum.UNREAD){
            map.put("is_read","N");
        }else if(type==MesgEnum.READ){
            map.put("is_read","Y");

        }else if(type==MesgEnum.UNCHECK){
            map.put("is_check","N");
        }

        return super.getSlaveJdbc().selectOne("message.MesgMapper.selectMesgCount",map);
    }



    /**
     *获取指定类型集合
     */
    public List<PullMsg> getMessgList(int userId,int start, int pageSize){

        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);
        map.put("start",start);
        map.put("pageSize",pageSize);

        List<PullMsg> list= super.getSlaveJdbc().selectList("message.MesgMapper.selectMesgList",map);

        return list;
    }




    /**
     *更改消息状态
     */
    public boolean modifyMsgStatus(int userId, int msgId,MesgEnum type){

        Map<String,Object> map= Maps.newHashMap();
        map.put("user_id",userId);

        if(type==MesgEnum.READ){
            map.put("is_read","Y");
            map.put("id",msgId);
        }else if(type==MesgEnum.DELETE){
            map.put("is_delete","1");
            map.put("id",msgId);
        }else if(type==MesgEnum.CHECK){
            map.put("is_check","Y");

        }else{
            return false;
        }

        return 0 < super.getMasterJdbc().update("message.MesgMapper.updateMesgStatus",map);
    }




    /**
     *新增用户消息
     */
    public boolean addPullMesg(int send_user_id,int userId, String msgType, int msgId, String content){

        if(content==null || !"R,F,S,B,B_F,P".contains(msgType)){
            logger.error("param is empty msgType={},content={}",msgType,content);
        }

        Map<String,Object> map= Maps.newHashMap();
        map.put("send_user_id",send_user_id);
        map.put("user_id",userId);
        map.put("object_type",msgType);
        map.put("object_id",msgId);
        map.put("content",content);

        return 1== super.getMasterJdbc().insert("message.MesgMapper.insertPullMessage", map);
    }



    /**
     *查看反馈回复list
     */
    public List<Map<String, Object>> getReplyList(int mesgId){

        Map<String,Object> map= Maps.newHashMap();
        map.put("object_id",mesgId);
        map.put("object_type","S");

       return super.getSlaveJdbc().selectList("message.MesgMapper.selectReplyList", map);
    }
}
